//
//  HFOLsectionBar.m
//  HFOuLiao
//
//  Created by haifeng on 16/7/6.
//  Copyright © 2016年 sangziwangluo. All rights reserved.
//

#import "HFOLsectionBar.h"


@interface HFOLsectionBar ()

@property (nonatomic, assign)CGFloat itemWidth;
@property (nonatomic, strong)NSMutableArray *titileBtnArray;
@property (nonatomic, strong)UIView *indicatorView;
@property (nonatomic, strong)UIButton *selectBtn;

@end

@implementation HFOLsectionBar
#pragma mark - life cycle
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initAll];
    }
    return self;

}

- (void)layoutSubviews{
    CGRect rect = self.frame;
    rect.size.height = self.frame.size.height;
    rect.size.width = _itemWidth * _titleArray.count;
    self.contentSize = rect.size;
    _indicatorView.frame = CGRectMake(15, (-self.frame.origin.y)+self.frame.size.height-17, _itemWidth - 30, 2);
}

- (void)initAll{
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    self.autoresizingMask = NO;
    self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.9];
    
    _indicatorView = [[UIView alloc] init];
    _indicatorView.backgroundColor = [UIColor blackColor];
    _indicatorView.frame = CGRectZero;
    [self addSubview:_indicatorView];
   }


#pragma mark - get&set
- (void)setTitleArray:(NSMutableArray *)titleArray{
    _titleArray = [titleArray mutableCopy];
    _titileBtnArray = [[NSMutableArray alloc] init];
    _itemWidth = self.frame.size.width/titleArray.count;
    CGFloat height = self.frame.size.height;
    
    // 创建按钮
    for (int i = 0; i < titleArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(_itemWidth * i, -self.frame.origin.y, _itemWidth, height);
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.contentEdgeInsets = UIEdgeInsetsMake(-8, 0, 8, 0);
        btn.tag = i;
    // 添加按钮
        [self addSubview:btn];
        [_titileBtnArray addObject:btn];
    }
    _selectBtn = _titileBtnArray[0];
}


- (void)btnClick:(UIButton *)send{
    if (send != _selectBtn) {
        _selectBtn = send;
        CGRect rect = _indicatorView.frame;
        rect.origin.x = send.frame.origin.x + 15;
        [UIView animateKeyframesWithDuration:0.25 delay:0 options:UIViewKeyframeAnimationOptionLayoutSubviews animations:^{
            _indicatorView.frame = rect;
        } completion:nil];
        
    }
    if ([self.sectionBardelegate respondsToSelector:@selector(sectionBar:selectButtonIndex:)]) {
        [self.sectionBardelegate sectionBar:self selectButtonIndex:send.tag];
    }
    
    NSLog(@"点击了按钮-----%li",send.tag);
}


- (void)setOffset:(CGFloat)offset{
    _offset = offset;
    CGRect rect1 = _indicatorView.frame;
    rect1.origin.x = [UIScreen mainScreen].bounds.size.width/4 * offset + 15;
    _indicatorView.frame = rect1;
}

- (void)setPag:(NSInteger)pag{
    _pag = pag;
    for (UIButton *btn in _titileBtnArray) {
        if (pag == btn.tag) {
            [self btnClick:btn];
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
